var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        adminId:'',
        groupId:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.adminId = Pub.getSession('adminId');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            adminId:_this.params.adminId
        };
        Api.adminlistdetails(paramsObj, function(data){
            console.log(data);
            $('#adminEmial').val(data.adminEmial);
            $('#adminName').val(data.adminName);
            $('#loginAccount').val(data.loginAccount);
            $('#remake').val(data.remake);
            setTimeout(function(){
                $('#roleName').val(data.groupId);
            },200)

        });
    },
    rolenamelist:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.rolenamelist(paramsObj, function(data){
            console.log(data);
            _this.rolenamelistList(data.list)
        });
    },
    rolenamelistList:function(data){
        var _this = this;
        if (data && data.length>0) {
            var html = '<option value="">请选择</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].groupId+'">'+data[i].roleName+'</option>';
            }
            $('#roleName').html(html);
            _this.rolenamelistEvent();
        };
    },
    rolenamelistEvent:function(){
        var _this = this;
        $('#roleName').on('change', function(event) {
            event.preventDefault();
            // _this.params.type=$(this).val();
        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {
            console.log($('#loginPassword').val().length);

            if ($('#loginPassword').val()!=$('#loginPasswordYes').val()) {
                layui.use('layer', function(){
                    layer.msg('两次填写的密码不相同', {icon: 5});
                });
            }else if($('#roleName option:selected').val()=='') {
                layui.use('layer', function(){
                    layer.msg('请选择所属部门', {icon: 5});
                });

            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    adminEmial:$('#adminEmial').val(),
                    adminName:$('#adminName').val(),
                    loginAccount:$('#loginAccount').val(),
                    roleName:$('#roleName option:selected').text(),
                    remake:$('#remake').val(),
                    loginPassword:$('#loginPassword').val()
                };
                var Obj = {
                    passport: _this.params.passport,
                    adminId:_this.params.adminId,
                    adminEmial:$('#adminEmial').val(),
                    adminName:$('#adminName').val(),
                    loginAccount:$('#loginAccount').val(),
                    remake:$('#remake').val(),
                    roleName:$('#roleName option:selected').text(),
                    loginPassword:$('#loginPassword').val()
                };
                console.log(Obj);
                if (Pub.getSession('adminEdit')==1) {
                    Api.adminmodify(Obj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('编辑成功', {icon: 6});
                            setTimeout(function(){
                                window.location.href = Pub.getHtmlUrl('./manage-member.html');
                            },1500)
                        });
                    });
                }else{
                     Api.adminadd(paramsObj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('添加成功', {icon: 6});
                            setTimeout(function(){
                                window.location.href = Pub.getHtmlUrl('./manage-member.html');
                            },1500)
                        });
                    });
                };
            };

        });
    },
    initEvent: function(){
        var _this = this;
        if (Pub.getSession('adminEdit')==1) {
            _this.getData();
            $('.slide-title').text('编辑成员');

        };
        _this.otherEvent();
        _this.rolenamelist();
        _this.submit();
    }
};
var page = new Page();